﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace JAAF.UI.WPF
{
    /// <summary>
    /// A <see cref="System.Windows.Data.IValueConverter"/> implementation that is used for 
    /// binding a value to the <see cref="UIElement.Visibility">Visibility property</see> 
    /// of a WPF / Silverlight user interface element.
    /// A non-null value makes the element visible, while a null value makes the element <b>Collapsed</b>.
    /// </summary>
    public class NotNullValueVisibilityConverter: IValueConverter
    {
        /// <summary>
        /// Converts a value to a <see cref="System.Windows.Visibility"/> value.
        /// </summary>
        /// <param name="_oSourceValue">The value produced by the binding source.</param>
        /// <param name="_oTargetType">The type of the binding target property.</param>
        /// <param name="_oParameter">The converter parameter to use.</param>
        /// <param name="_oCulture">The culture to use in the conversion.</param>
        /// <returns>
        /// <see cref="System.Windows.Visibility.Visible"/> if the source value is not null
        /// and <see cref="System.Windows.Visibility.Collapsed"/> if the source value is null.
        /// </returns>
        public object Convert(object _oSourceValue, Type _oTargetType, object _oParameter, System.Globalization.CultureInfo _oCulture)
        {
            if (_oTargetType != typeof(System.Windows.Visibility))
                throw new InvalidOperationException("Can convert only to System.Windows.Visibility values.");

            if (_oSourceValue != null)
                return System.Windows.Visibility.Visible;
            else
                return System.Windows.Visibility.Collapsed;
        }

        /// <summary>
        /// This conversion is not supported
        /// </summary>
        /// <param name="_oValue"></param>
        /// <param name="_oTargetType"></param>
        /// <param name="_oParameter"></param>
        /// <param name="_oCulture"></param>
        /// <returns></returns>
        public object ConvertBack(object _oValue, Type _oTargetType, object _oParameter, System.Globalization.CultureInfo _oCulture)
        {
            throw new NotSupportedException();
        }
    }
}
